package com.longtengyuyun.socke_demo.sock;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author ZhaoYunLong
 * @date 2022/4/14
 * @description
 */
public class SocketServer {


    public static void main(String[] args) {

        try {
            int port = 5555;
            ServerSocket serverSocket = new ServerSocket(port);

            System.out.println("等待连接的到来");

            Socket accept = serverSocket.accept();
            InputStream inputStream = accept.getInputStream();

            byte[] bytes = new byte[1024];
            int len;
            while ((len = inputStream.read(bytes))!= -1){
                String content = new String(bytes, 0, len, "utf-8");
                System.out.println("len = " + len + ", content: " + content);
            }
            inputStream.close();
            accept.close();
            serverSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
